package com.leo;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @ClassName ButtonEventDemo
 * @Description button 事件处理
 * @Author wangss
 * @date 2020.05.16 22:56
 * @Version 1.0
 */
public class ButtonEventDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final Button button = new Button();
        button.setText("这是按钮");

        // 设置按钮在group中的xy位置
        button.setLayoutX(10d);
        button.setLayoutY(10d);

        // 设置按钮的大小
        button.setPrefHeight(50d);
        button.setPrefWidth(200d);

        // 设置button的字体
        button.setFont(Font.font(30d));

        // 单击事件
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("单击事件");
                // 返回按钮
                Button button1 = (Button) event.getSource();
                System.out.println("获取到组件 = " + button1.getText());
            }
        });

        // 双击事件
        button.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("鼠标单击");
                if (event.getClickCount() == 2 && MouseButton.PRIMARY.name().equals(event.getButton().name())) {
                    System.out.println("鼠标左键双击");
                }
                System.out.println("鼠标按键 = " + event.getButton().name());
            }
        });

        // 键盘事件
        button.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                // event.getCode().getName() 和 event.getCode().name() 获取到的值有些键位不同
                System.out.println("按下的键为 = " + event.getCode().getName());
                if (KeyCode.A.getName().equals(event.getCode().getName())) {
                    System.out.println("你好，我好开心！！！");
                }
            }
        });
        button.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                System.out.println("释放的键为 = " + event.getCode().name());
            }
        });

        final Group group = new Group();
        // 增加组件
        group.getChildren().addAll(button);

        Scene scene = new Scene(group);

        primaryStage.setScene(scene);
        primaryStage.setTitle("button");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(500d);
        primaryStage.show();
    }
}
